package cn.itcast.controller.system;

import cn.itcast.controller.BaseContorller;
import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.RoleService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.olap4j.impl.ArrayMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseContorller {

    @Autowired
    private RoleService roleService;
    @Autowired
    private ModuleService moduleService;


    @RequestMapping(value = "/list",name = "分页展示部门数据显示")
    public String findAll(@RequestParam(value = "page",defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "5") Integer pageSize){
        PageInfo pageInfo =roleService.findPage(getCompanyId(),pageNum,pageSize);
        request.setAttribute("page",pageInfo);
        return "system/role/role-list";
    }

    @RequestMapping(value = "/toAdd",name = "跳转添加部门")
    public String toAdd(){
        List<Role> roleList = roleService.findAll(getCompanyId());
        request.setAttribute("roleList",roleList);
        return "system/role/role-add";
    }


    @RequestMapping(value = "/edit",name = "保存部门")
    public String edit(Role role){


        if (StringUtils.isEmpty(role.getId())){//id为空
            role.setId(UUID.randomUUID().toString());
            role.setCompanyId(getCompanyId());
            role.setCompanyName(getCompanyName());
            roleService.save(role);

        }else {//id不为空
            roleService.update(role);
        }


        return "redirect:/system/role/list.do";
    }

    @RequestMapping(value = "/toUpdate",name = "进入部门修改页面")
    public String update(String id){
        List<Role> roleList = roleService.findAll(getCompanyId());
        request.setAttribute("roleList",roleList);
        Role role = roleService.findById(id);
        request.setAttribute("role",role);
        return "system/role/role-add";
    }

    @RequestMapping(value = "/delete",name = "进入部门修改页面")
    public String delete(String id){

        roleService.deleteById(id);

        return "redirect:/system/role/list.do";
    }

    @RequestMapping(value = "/roleModule",name = "进入角色权限配置页面")
    public String roleModule(String roleid){
        Role role = roleService.findById(roleid);
        request.setAttribute("role",role);
        return "system/role/role-module";
    }
    @RequestMapping(value = "/getZtreeNodes",name = "角色权限数据")
    @ResponseBody       //返回ajax
    public List<Map> getZtreeNodes(String roleid){
        //查询全部模块
        List<Module> moduleList = moduleService.findAll();
        //构造Map集合
        List<Map> list = new ArrayList<>();
        //查找已存在的拥有的权限
        List<String> moduleIdList = moduleService.findByRoleId(roleid);
        //构造map
        Map map =null;

        //遍历模块
        for (Module module : moduleList) {
            map = new HashMap();
            map.put("id",module.getId());
            map.put("pId",module.getParentId());
            map.put("name",module.getName());
            if (moduleIdList.contains(module.getId())){
                map.put("checked",true);
            }
            list.add(map);
        }
        return list;
    }


    @RequestMapping(value = "/updateRoleModule",name="提交角色权限修改结果")
    public String updateRoleModule(String roleid,String moduleIds){

        roleService.updateRoleModule(roleid,moduleIds);

        return "redirect:/system/role/list.do";
    }


}
